import html2canvas from "html2canvas"
import JSPDF from "jspdf"
export default {
  install (Vue, options) {
    Vue.prototype.ExportSavePdf = function (title, dom) {

      var title = title
      var ST = document.documentElement.scrollTop;
      var SL = document.documentElement.scrollLeft;
      document.documentElement.scrollLeft = 0;
      document.documentElement.scrollTop = 0;
      var dom = document.querySelector("#pdfContent")
      var items = dom.getElementsByClassName('item')
      items = Array.from(items)
      let lastHeight = items[items.length - 1].offsetHeight + 10
      items = Array.from(new Set(items.map(item => item.offsetTop-66)))
      items[items.length - 1] = items[items.length - 1] + lastHeight
      var pagesHeight = []
      html2canvas(document.querySelector('#pdfContent'), {
        useCORS: true,
      }).then(function (canvas) {
        document.documentElement.scrollTop = ST;
        document.documentElement.scrollLeft = SL;
        var pdf = new JSPDF('p', 'mm', 'a4');
        var ctx = canvas.getContext('2d')
        var a4w = 190, a4h = 277
        var imgHeight = Math.floor(a4h * canvas.width / a4w)
        var itemsMax = Math.max.apply(null, items)
        var getPageHeight = function () {
          var pagesMax = pagesHeight[pagesHeight.length - 1] || 0
          if (itemsMax - pagesMax <= imgHeight) {
            pagesHeight.push(itemsMax)
          } else {
            let arr = items.filter(item => item <= (imgHeight + pagesMax))
            pagesHeight.push(Math.max.apply(null, arr))
            getPageHeight()
          }
        }
        getPageHeight()
        var renderedHeight = 0;
        for (var i in pagesHeight) {
          var page = document.createElement("canvas");
          let height = i == 0 ? pagesHeight[i] : pagesHeight[i] - pagesHeight[i - 1]
          page.width = canvas.width;
          page.height = height
          page.getContext('2d').putImageData(ctx.getImageData(0, renderedHeight, canvas.width, height), 0, 0);
          var imageData = page.getContext('2d').getImageData(0, 0, canvas.width, height);
          for (var i = 0; i < imageData.data.length; i += 4) {
            if (imageData.data[i + 3] == 0) {
              imageData.data[i] = 255;
              imageData.data[i + 1] = 255;
              imageData.data[i + 2] = 255;
              imageData.data[i + 3] = 255;
            }
          }
          page.getContext('2d').putImageData(imageData, 0, 0);
          if (i > 0) {
            page.getContext('2d').fillStyle = '#ffffff';
            page.getContext('2d').fillRect(10, 0, 774, 1);
          }
          pdf.addImage(page.toDataURL('image/jpeg', 1.0), 'JPEG', 10, 10, a4w, Math.min(a4h, a4w * page.height / page.width));
          renderedHeight += height;
          console.log(renderedHeight);
          console.log(height);
          console.log(imageData);
          console.log(pagesHeight);
          console.log(itemsMax);
          console.log(imgHeight);
          if (renderedHeight < itemsMax) {
            pdf.addPage();
          }
        }
        pdf.save(title + '.pdf')
      })
    }
  }
}
